package cn.chy.com.springbootdemo2.controller;

import cn.chy.com.springbootdemo2.base.annotation.IgnoreCommonResponseAnnotation;
import cn.chy.com.springbootdemo2.dto.DeptDTO;
import cn.chy.com.springbootdemo2.pojo.Department;
import cn.chy.com.springbootdemo2.pojo.UserInfo;
import cn.chy.com.springbootdemo2.service.DepartmentService;
import cn.chy.com.springbootdemo2.vo.CommonsResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static jdk.nashorn.internal.runtime.PropertyDescriptor.GET;

/**
 * @Description: TODO
 * @author: 颜景琦
 * @date: 2021年10月25日 9:12
 */
@RestController
@CrossOrigin
@Validated
@RequestMapping("/dept")
@Api(description = "部门控制层2")
public class DeptController {

    @Resource
    private DepartmentService departmentService;

    @GetMapping("all")
    @IgnoreCommonResponseAnnotation
    @ApiOperation(value = "查询所有部门", httpMethod = "GET", response = CommonsResponse.class, produces = "application/json")
    public Object all() {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        return departmentService.list(wrapper);
    }

    @GetMapping("page/{current}")
    @ApiOperation(value = "查询部门分页",
            httpMethod = "GET",
            response = CommonsResponse.class, produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, dataType = "int", paramType = "path")
    })
    public IPage<Department> insertOne(@PathVariable int current) {
        IPage<Department> page = new Page<>(current, 3);
        page = departmentService.page(page);
        return page;
    }


    @PostMapping("add")
    public boolean add(@RequestBody @Validated DeptDTO deptDTO) {
        Department department = new Department();
        BeanUtils.copyProperties(deptDTO, department);

        boolean save = departmentService.save(department);
        return save;
    }


    @DeleteMapping("del/{id}")
    public boolean patterOne(@PathVariable String id) {

        return departmentService.removeById(id);
    }


    @PutMapping("edit")
    public boolean updateOne(@RequestBody DeptDTO deptDTO) {

        Department department = new Department();
        BeanUtils.copyProperties(deptDTO, department);
        System.out.println(department.getId());
        System.out.println(department.getName());

        return departmentService.updateById(department);
    }
}
